package com.zd.commodity.userMng.dao.impl;

import java.util.Set;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;

import com.zd.commodity.dao.impl.BaseDao;
import com.zd.commodity.userMng.dao.IRoleDao;
import com.zd.commodity.userMng.entity.Role;
import com.zd.commodity.userMng.entity.User;

public class RoleDao extends BaseDao<Role> implements IRoleDao {

	public void delete(Long id)
	{
		Role role =  getHibernateTemplate().get(Role.class, id);
		Set<User> users = role.getUsers();
		for (User user : users) {
			user.setRole(null);
		}
		role.setUsers(null);
		getHibernateTemplate().getSessionFactory().getCurrentSession().delete(role);
	}
	
	@Override
	public DetachedCriteria getDetachedCriteria(Role t1, Role t2, Object param) {
		DetachedCriteria dc=DetachedCriteria.forClass(Role.class);
		if(t1!=null){
			if(!"".equals(t1.getRolename())){
				dc.add(Restrictions.like("rolename", t1.getRolename(), MatchMode.ANYWHERE));
			}
		}
		return dc;
	}

}
